package com.wuliu.action.person;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import com.wuliu.action.BaseAction;
import com.wuliu.business.person.PersonObj;
import com.wuliu.model.common.PageVO;
import com.wuliu.model.common.QueryVO;
import com.wuliu.model.person.Person;
import com.wuliu.model.sys.User;
import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbUtil;

public class PersonAction extends BaseAction {
	private Person person;
	
    public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String personList() {  
		List<QueryVO> list = new ArrayList<QueryVO>(); 
        QueryVO PER_ID = new QueryVO("A.PER_ID", "选择", "checkbox", "chioce", "");
        list.add(PER_ID); 
        
        QueryVO  PER_CODE = new QueryVO("A.PER_CODE", "员工编码", "1");
        list.add(PER_CODE);
        
        QueryVO  PER_NAME = new QueryVO("A.PER_NAME", "员工名称", "1");
        list.add(PER_NAME);
        
        QueryVO  PER_GENDER = new QueryVO("A.PER_GENDER", "性别", "1");
        list.add(PER_GENDER);
        
        QueryVO  PER_BIRTH = new QueryVO("A.PER_BIRTH", "出生日期", "1");
        list.add(PER_BIRTH);
        
        QueryVO  PER_MAIL = new QueryVO("A.PER_MAIL", "邮箱", "1");
        list.add(PER_MAIL);
        
        QueryVO  PER_ADDR = new QueryVO("A.PER_ADDR", "员工住址", "1");
        list.add(PER_ADDR);
        
        QueryVO PER_TEL = new QueryVO("A.PER_TEL", "员工电话", "1");
        list.add(PER_TEL);
        
        QueryVO PER_STATUS = new QueryVO("A.PER_STATUS", "员工状态", "1");
        list.add(PER_STATUS);
        
        QueryVO ADD_BY = new QueryVO("A.ADD_BY", "创建人", "1");
        list.add(ADD_BY);  
        
        QueryVO ADD_DATE = new QueryVO("A.ADD_DATE", "创建日期", "1");
        list.add(ADD_DATE);
        
        QueryVO ADD_TIME = new QueryVO("A.ADD_TIME", "创建时间", "1");
        list.add(ADD_TIME);
        
        QueryVO UPD_BY = new QueryVO("A.UPD_BY", "最后修改人", "1");
        list.add(UPD_BY);  
        
        QueryVO UPD_DATE = new QueryVO("A.UPD_DATE", "最后修改日期", "1");
        list.add(UPD_DATE);
        
        QueryVO UPD_TIME = new QueryVO("A.UPD_TIME", "最后修改时间", "1");
        list.add(UPD_TIME);
        

        String intStr = "1000";// 需要特殊显示的标记;在原来基础上加1000 
        String tableName = "  WL_PERSON A WHERE 1=1 ";  
 	    
        String orderStr = " ORDER BY A.PER_NAME ASC "; 
       
        HashMap<String, String> hm = null; //DBHelper.formQueryToken(request);
        HashMap<String, String> hmSearch = new HashMap<String, String>();
        String searchFlag = BaseUtil.getStr(request.getParameter("searchFlag"));
        String s_per_name = BaseUtil.getStr(request.getParameter("s_per_name"));
        String s_per_code = BaseUtil.getStr(request.getParameter("s_per_code"));  
        
    	 if(!s_per_name.equals("")){
           	tableName += " AND A.PER_NAME LIKE '%"+s_per_name+"%' "; 
           	hmSearch.put("s_per_name", s_per_name);
         }
    	 if(!s_per_code.equals("")){
           	tableName += " AND A.PER_CODE LIKE '%"+s_per_code+"%'";
           	hmSearch.put("s_per_code", s_per_code);
         } 
    	 
        String pageNum = BaseUtil.getStr(request.getParameter("pageNums"));
        if(pageNum==null||pageNum.equals(""))
            pageNum = "1"; 
        List<?> resultL = DbUtil.queryStrForPage(list, tableName, intStr, hm, orderStr, Integer.parseInt(pageNum));
        int queryCount = DbUtil.queryStrCount(list, tableName, intStr, hm, orderStr);
        PageVO vo = DbUtil.pageObjFor(resultL, pageNum, queryCount);  
        
        request.setAttribute("hmSearch", hmSearch);
        request.setAttribute("searchFlag", searchFlag); 
         
        request.setAttribute("voResult", vo);
        request.setAttribute("qList", list);// 显示名称 
        return "personList";
	}
    
    public String savePerson(){

    	Person person_e	= PersonObj.getPersonById(person.getPerCode());
    	if(person_e.getPerCode()==null||person.getPerCode().equals("")){
    		String sql	= "INSERT INTO WL_PER(PER_ID,PER_CODE,PER_NAME,PER_SPOT,PER_GENDER," +
    				"PER_BIRTH,PER_ADDR,PER_TEL,PER_MAIL,PER_CARDID,ENTER_DATE,PER_JOB," +
    				"PER_STATUS,ADD_DATE,ADD_TIME,ADD_BY) " +
    				" VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,DATE_FORMAT(NOW(),'%Y%m%d')," +
    				" DATE_FORMAT(NOW(),'%H%i%s'),?)";
    		List<String> lt = new ArrayList<String>();
    		lt.add(person.getPerCode());
    		lt.add(person.getPerCode());
    		lt.add(person.getPerName());
    		lt.add(person.getPerSpot());
    		lt.add(person.getPerGender());
    		lt.add(person.getPerBirth());
    		lt.add(person.getPerAddr());
    		lt.add(person.getPerTel());
    		lt.add(person.getPerMail());
    		lt.add(person.getPerCardid());
    		lt.add(person.getEnterDate());
    		lt.add(person.getPerJob());
    		lt.add(person.getPerStatus());
    		lt.add(((User)session.get("user")).getUserName());
    		boolean result	= DbUtil.excuSQL(lt, sql);
    		if(result){
    			request.setAttribute("showMsg","新增成功！");
    		}else{
    			request.setAttribute("showMsg","新增失败！");
    		} 		
    	}else{
    		request.setAttribute("showMsg","该编号已存在！");
    	}
    	
    	return personList();
    }
    
    public String updatePerson(){
		String sql	= "UPDATE WL_PER SET PER_NAME=?,PER_SPOT=?,PER_GENDER=?,PER_BIRTH=?,PER_ADDR=?," +
				"PER_TEL=?,PER_MAIL=?,PER_CARDID=?,ENTER_DATE=?,PER_JOB=?,PER_STATUS=?," +
				"UPD_DATE=DATE_FORMAT(NOW(),'%Y%m%d'), UPD_TIME=DATE_FORMAT(NOW(),'%H%i%s')," +
				"UPD_BY=? WHERE PER_CODE=? ";
		List<String> lt = new ArrayList<String>();
		
		lt.add(person.getPerName());
		lt.add(person.getPerSpot());
		lt.add(person.getPerGender());
		lt.add(person.getPerBirth());
		lt.add(person.getPerAddr());
		lt.add(person.getPerTel());
		lt.add(person.getPerMail());
		lt.add(person.getPerCardid());
		lt.add(person.getEnterDate());
		lt.add(person.getPerJob());
		lt.add(person.getPerStatus());
		lt.add(((User)session.get("user")).getUserName());
		lt.add(person.getPerCode());
		boolean result	= DbUtil.excuSQL(lt, sql);
		if(result){
			request.setAttribute("showMsg","更新成功！");
		}else{
			request.setAttribute("showMsg","更新失败！");
		} 		

    	return personList();
    }
    
    public String getPersonInfo(){
    	String id	= BaseUtil.getStr(request.getParameter("id"));
    	Person person = PersonObj.getPersonById(id);

    	JSONObject json = JSONObject.fromObject(person);
    	try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
	 public String delPerson(){
		 String id	= request.getParameter("id");

		 String sql	= "DELETE WL_PERSON WHERE PER_ID ='"+id+"'";

		 boolean result	= DbUtil.execute(sql);
		 if(result){
			 request.setAttribute("showMsg","删除成功！");
		 }else{
			 request.setAttribute("showMsg", "删除失败！");
		 }
		 return personList();
	 }
}
